//
//  ButtonTableViewCell.m
//  HYHOME
//
//  Created by cjy on 16/3/3.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ButtonTableViewCell.h"
#import "DKNightVersion.h"
#import "UIImageView+WebCache.h"
#import "ModelS.h"
#import "Layout.h"
#import "NetHandleBlock.h"
@interface ButtonTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic,retain)NSMutableArray *array; /**< model数组.*/
@property (nonatomic,retain)NSMutableArray *arrpic;
@end


@implementation ButtonTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self handle];

        [self createCollectionView];
    }
    
    return self;
}

- (void)handle{
   
    self.array = [NSMutableArray array];

    NSString *str = @"http://tuku-wap.m.jia.com/v1.2.4/hybrid/label/ext/search";
    
    [NetHandleBlock networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *array = [result objectForKey:@"label_list"];
        for (NSDictionary *dic in array) {
            
            
            NSDictionary *dicI = [dic objectForKey:@"image"];
            ModelS *model = [[ModelS alloc]init];
            
            model.label_name = [dic objectForKey:@"label_name"];
            model.url = [dicI objectForKey:@"url"];
            model.article_id = [dic objectForKey:@"id"];
            [self.array addObject:model];
            
            
            
            [model release];
            
            
        }
        
        [self.collectionV reloadData];
        
    }];
    
    
  }


- (void)createCollectionView{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake((self.contentView . frame.size.width)/5.0, (self.contentView.frame.size.width )/ 4.0);
//
    self.flowLayout.minimumInteritemSpacing = 5;
    self.flowLayout.minimumLineSpacing = 5;
//
    self.flowLayout.sectionInset = UIEdgeInsetsMake(15,20, 10, 20);
    
    
    
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width,180) collectionViewLayout:self.flowLayout];
    NSLog(@"qqqqq%f",self.collectionV.frame.size.width);
    NSLog(@"%f",self.contentView.frame.size.width);
    
    [self.contentView addSubview:self.collectionV];
    
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    
    [self.collectionV registerClass:[Layout class] forCellWithReuseIdentifier:@"pool1"];
    
    self.collectionV.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    [self.contentView addSubview:self.collectionV];
    
    
    self.collectionV.showsHorizontalScrollIndicator = NO;
    
    self.collectionV.userInteractionEnabled = YES;
    [self.collectionV release];
    [self.flowLayout release];
    
}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return    self.array.count ;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    Layout *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    
    ModelS *model = [self.array objectAtIndex:indexPath.item];
    
    [cell passModel:model];
    
    return cell;
    
    
    
}


/** 通过 delegate 回调出去.*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(didClickItem:)]) {
        
        [self.delegate didClickItem:[self.array objectAtIndex:indexPath.item]];
        
    }
    
    
    
   
    
    
    
}



- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    return layoutAttributes;
    
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGRect frame = self.collectionV.frame;
    frame.size.width = self.contentView.bounds.size.width;
    self.collectionV.frame = frame;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
